﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using WeiXinPF.DBUtility;
using WeiXinPF.Common;//Please add references

namespace WeiXinPF.DAL
{
    public partial class wx_wechat_matrix
    {
        public wx_wechat_matrix() { }

        /// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(WeiXinPF.Model.wx_wechat_matrix model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into wx_wechat_matrix(");
            strSql.Append("wid,title,catelog,img_link,matrix_link,isdelete,createdate,modifydate,remark)");
            strSql.Append(" values (");
            strSql.Append("@wid,@title,@catelog,@img_link,@matrix_link,@isdelete,@createdate,@modifydate,@remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@wid", SqlDbType.Int,4),
                    new SqlParameter("@title", SqlDbType.VarChar,100),
                    new SqlParameter("@catelog", SqlDbType.VarChar,100),
                    new SqlParameter("@img_link", SqlDbType.VarChar,500),
                    new SqlParameter("@matrix_link", SqlDbType.VarChar,500),
                    new SqlParameter("@isdelete", SqlDbType.Bit,1),
                    new SqlParameter("@createdate", SqlDbType.DateTime),
                    new SqlParameter("@modifydate", SqlDbType.DateTime),
                    new SqlParameter("@remark", SqlDbType.VarChar,1000)
                    };
            parameters[0].Value = model.wid;
            parameters[1].Value = model.title;
            parameters[2].Value = model.catelog;
            parameters[3].Value = model.img_link;
            parameters[4].Value = model.matrix_link;
            parameters[5].Value = model.isdelete;
            parameters[6].Value = DateTime.Now;
            parameters[7].Value = DateTime.Now;
            parameters[8].Value = model.remark;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(WeiXinPF.Model.wx_wechat_matrix model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update wx_wechat_matrix set ");
            strSql.Append("wid=@wid,");
            strSql.Append("title=@title,");
            strSql.Append("catelog=@catelog,");
            strSql.Append("img_link=@img_link,");
            strSql.Append("matrix_link=@matrix_link,");
            strSql.Append("isdelete=@isdelete,");
            strSql.Append("createdate=@createdate,");
            strSql.Append("modifydate=@modifydate, ");
            strSql.Append("remark=@remark ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@wid", SqlDbType.Int,4),
                    new SqlParameter("@title", SqlDbType.VarChar,100),
                    new SqlParameter("@catelog", SqlDbType.VarChar,100),
                    new SqlParameter("@img_link", SqlDbType.VarChar,500),
                    new SqlParameter("@matrix_link", SqlDbType.VarChar,500),
                    new SqlParameter("@isdelete", SqlDbType.Bit),
                    new SqlParameter("@createdate", SqlDbType.DateTime),
                    new SqlParameter("@modifydate", SqlDbType.DateTime),
                     new SqlParameter("@remark", SqlDbType.VarChar,1000),
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.wid;
            parameters[1].Value = model.title;
            parameters[2].Value = model.catelog;
            parameters[3].Value = model.img_link;
            parameters[4].Value = model.matrix_link;
            parameters[5].Value = model.isdelete;
            parameters[6].Value = model.createdate;
            parameters[7].Value = model.modifydate;
            parameters[8].Value = model.remark;
            parameters[9].Value = model.id;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update wx_wechat_matrix ");
            strSql.Append(" set isdelete=1 where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public WeiXinPF.Model.wx_wechat_matrix DataRowToModel(DataRow row)
        {
            WeiXinPF.Model.wx_wechat_matrix model = new WeiXinPF.Model.wx_wechat_matrix();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["wid"] != null && row["wid"].ToString() != "")
                {
                    model.wid = int.Parse(row["wid"].ToString());
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["catelog"] != null)
                {
                    model.catelog = row["catelog"].ToString();
                }
                if (row["img_link"] != null)
                {
                    model.img_link = row["img_link"].ToString();
                }
                if (row["matrix_link"] != null)
                {
                    model.matrix_link = row["matrix_link"].ToString();
                }
                if (row["isdelete"] != null)
                {
                    model.isdelete = (bool)row["isdelete"];
                }
                if (row["createdate"] != null)
                {
                    model.createdate = DateTime.Parse(row["createdate"].ToString());
                }
                if (row["modifydate"] != null)
                {
                    model.modifydate = DateTime.Parse(row["modifydate"].ToString());
                }
                if (row["remark"] != null)
                {
                    model.remark = row["remark"].ToString();
                }
            }
            return model;
        }


        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,wid,title,catelog,img_link,matrix_link,isdelete,createdate,modifydate,remark ");
            strSql.Append(" FROM wx_wechat_matrix ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere );
            }
           
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from wx_wechat_matrix T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }




    }
}
